package com.szrt.session;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;


public class UserController extends HttpServlet{
	
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		UserEntity user=new UserEntity();
		String username=request.getParameter("username");
		
		user.setId(Integer.parseInt(request.getParameter("id")));
		user.setName( request.getParameter("username"));
		user.setPassword( request.getParameter("password"));
		
		
		Set<String> users=new HashSet<String>();
		Map<String,Object> map=new HashMap<String,Object>();
		if("刘".equals(user.getName()) && "hao".equals(user.getPassword())){
			
			//一旦用户登录，给用户一个篮子 Seesion
			HttpSession session=request.getSession();
			//为唯一的id
//			String id = session.getId();
			users.add(username);
			int size = users.size();
//			request.setAttribute("cou", size);
			request.getServletContext().setAttribute("cou", size);
			
			session.setAttribute("user", username);

			
			
			
			
			map.put("code", 200);
		}else{
			map.put("code", 333);
		}
		
		Gson json=new Gson();
		String jsonString = json.toJson(map);
//		给前台返回json数据
		response.getWriter().write(jsonString);
	}
	

}
